package com.realm;

import com.entity.Employee;
import com.entity.Menu;
import com.entity.Role;

import com.mapper.setting.SysEmployeeMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private SysEmployeeMapper sysUserMapper;

/*    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenMapper menMapper;*/
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

     /*   //获取登录成功后的账户名 -- 根据账户名查询对应的账户id，
        String username =  principalCollection.getPrimaryPrincipal().toString();
        SysUser sys_user = sysUserMapper.getByName(username);
        //RBAC基于角色的权限控制
        //根据用户id查询对应角色信息
        List<Role> roleList = roleMapper.getRolesByUserId(sys_user.getId());
        //角色用Set来装，目的是去重，还有info对象中需要Set格式
        Set<String> roleSets = new HashSet<>();
        //获取角色ID
        List<Integer> role_ids = new ArrayList<>();
        for (Role role:roleList) {
            roleSets.add(role.getRoleName());
            role_ids.add(role.getRoleId());
        }
        List<Menu> menuByUserId = menMapper.getMenuByUserId(sys_user.getId());
        Set<String> stringSet=new HashSet<>();
        if (menuByUserId!=null&&menuByUserId.size()>0){
            for (Menu u: menuByUserId) {
                stringSet.add(u.getTitle());
            }
        }

        //根据账户id查询对应的角色
        //根据对应的角色id查询对应权限
      *//*  Set<String> sys_menuSets = new HashSet<>();
        List<Sys_Menu> sys_menus = sysRoleMapper.findMenuListByRoleId(role_ids);
        for (Sys_Menu sm:sys_menus) {
            sys_menuSets.add(sm.getTitle());
        }*//*
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //根据账户查询数据库中对应账户的角色
        info.setRoles(roleSets);
        info.setStringPermissions(stringSet);
            //根据账户查询数据库中对应账户的权限
*//*        info.setStringPermissions(sys_menuSets);*//*
        return info;
*/



    return null;
    }
    //认证

    /**
     * authenticationToken 这里的这个就是service里设置的userNamePasswordToken
     * 我们在这一步获取到数据库的数据，与前端传过来的东西做效验！
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String  password = String.valueOf(authenticationToken.getCredentials());  //这是密码
        String  userName = String.valueOf(authenticationToken.getPrincipal());// 这是用户名

        try {
            Employee byName = sysUserMapper.findEmpByName(userName);
            System.out.println(byName);
            System.out.println(byName.getEmpPassword());
            if (byName!=null){
                SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(userName,byName.getEmpPassword(),"myRealm");
                simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("gxa"));
                return  simpleAuthenticationInfo;
            }
        }catch (Exception e){
            e.printStackTrace();
        }



        return null;
    }
}
